home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / fsm.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  77 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from observe import Observable
  5.  
  6. class StateMachine(Observable):
  7.     
  8.     def __init__(self, name, states_list, start_state = None):
  9.         Observable.__init__(self)
  10.         self.nodes = { }
  11.         self.name = name
  12.         for state in states_list:
  13.             self.nodes[state] = { }
  14.         
  15.         self._current_state = None
  16.         self.state = start_state
  17.  
  18.     
  19.     def create_trans(self, from_state, to_state, input):
  20.         if not isinstance(input, basestring):
  21.             for iota in input:
  22.                 self.nodes[from_state][iota] = to_state
  23.             
  24.         else:
  25.             self.nodes[from_state][input] = to_state
  26.  
  27.     
  28.     def process(self, input):
  29.         if self.state not in self.nodes:
  30.             return None
  31.         
  32.         
  33.         try:
  34.             curr_node = self.nodes[self.state]
  35.         except Exception:
  36.             e = None
  37.             print self.nodes.keys(), self.state
  38.             raise 
  39.  
  40.         if input in curr_node:
  41.             self.state = curr_node[input]
  42.         
  43.         return self.state
  44.  
  45.     
  46.     def set_state(self, state):
  47.         oldstate = self._current_state
  48.         self._current_state = state
  49.         self.notify('state', oldstate, state)
  50.  
  51.     state = property((lambda self: self._current_state), set_state)
  52.  
  53.  
  54. class StateManager(list):
  55.     
  56.     def __init__(self, important_states = None):
  57.         list.__init__(self)
  58.         if not important_states:
  59.             pass
  60.         self.important_states = []
  61.  
  62.     
  63.     def add_machine(self, machine):
  64.         if machine not in self:
  65.             self.append(machine)
  66.             machine.add_observer(self.machine_changed, 'state')
  67.         
  68.  
  69.     
  70.     def machine_changed(self, src, attr, _old, new):
  71.         important_machines = _[1]
  72.         other_machines = _[2]
  73.         [ machine.process(src.name + '_' + new) for machine in important_machines ]
  74.         [ machine.process(src.name + '_' + new) for machine in other_machines ]
  75.  
  76.  
  77.